home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / utils / pltcl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  5.5 KB  |  188 lines

  1. /* $Id: pltcl.c,v 1.5 1994/08/25 04:05:16 mjl Exp $
  2.  * $Log: pltcl.c,v $
  3.  * Revision 1.5  1994/08/25  04:05:16  mjl
  4.  * Fixed error output; removes spurious <RET> at end.
  5.  *
  6.  * Revision 1.4  1994/07/19  22:33:16  mjl
  7.  * Internal header file inclusion changed to /not/ use a search path so that
  8.  * it will work better with makedepend.
  9.  *
  10.  * Revision 1.3  1994/06/30  18:55:50  mjl
  11.  * Minor changes to eliminate gcc -Wall warnings.
  12.  *
  13.  * Revision 1.2  1994/06/24  20:41:35  mjl
  14.  * Added error handler specific to pltcl.  Ensures output device is in text
  15.  * mode before issuing error message.
  16.  *
  17.  * Revision 1.1  1994/06/23  22:51:28  mjl
  18.  * A plotting interpreter that uses Tcl to drive PLplot primitives.  This can
  19.  * be used with virtually any PLplot output driver.  The executable is an
  20.  * extended tclsh that has been embellished with a (soon to be) large set
  21.  * of Tcl commands for executing PLplot graphics calls.  The scripts are not
  22.  * the same as those that plserver can execute, as the latter is object-based
  23.  * and uses widget commands, whereas pltcl uses global commands to drive
  24.  * PLplot.  The two style of commands are similar enough, however (differing
  25.  * only by an introducer) that a text filter could be used to go between
  26.  * them.
  27.  */
  28.  
  29. /*----------------------------------------------------------------------*\
  30.  * 
  31.  * pltcl.c --
  32.  *
  33.  *    Main program for Tcl-interface to PLplot.  Allows interpretive
  34.  *    execution of plotting primitives without regard to output driver.
  35.  *
  36.  * Maurice LeBrun
  37.  * IFS, University of Texas at Austin
  38.  * 19-Jun-1994
  39.  *
  40. \*----------------------------------------------------------------------*/
  41.  
  42. #include "plplotP.h"
  43. #include "pltcl.h"
  44.  
  45. static void
  46. plErrorHandler(Tcl_Interp *interp, int code, int tty);
  47.  
  48. extern void (*tclErrorHandler)(Tcl_Interp *interp, int code, int tty);
  49.  
  50. /*----------------------------------------------------------------------*\
  51.  * main --
  52.  *
  53.  * Just a stub routine to call pltclMain.  The latter is nice to have when
  54.  * building extended tclsh's, since then you don't have to rely on sucking
  55.  * the Tcl main out of libtcl (which doesn't work correctly on all
  56.  * systems/compilers/linkers/etc).  Hopefully in the future Tcl will
  57.  * supply a sufficiently capable tclMain() type function that can be used
  58.  * instead.
  59. \*----------------------------------------------------------------------*/
  60.  
  61. int
  62. main(int argc, char **argv)
  63. {
  64.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  65.  
  66.     exit(pltclMain(argc, argv));
  67. }
  68.  
  69. /*----------------------------------------------------------------------*\
  70.  * plExitCmd
  71.  *
  72.  * PLplot/Tcl extension command -- handle exit.
  73.  * The reason for overriding the normal exit command is so we can tell
  74.  * the PLplot library to clean up.
  75. \*----------------------------------------------------------------------*/
  76.  
  77. static int
  78. plExitCmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
  79. {
  80.  
  81. /* Print error message if one given */
  82.  
  83.     if (interp->result != NULL && interp->result[0] != '\0')
  84.     fprintf(stderr, "%s\n", interp->result);
  85.  
  86.     plspause(0);
  87.     plend();
  88.  
  89.     Tcl_UnsetVar(interp, "tcl_prompt1", 0);
  90.     Tcl_Eval(interp, "tclexit");
  91.  
  92.     return TCL_OK;
  93. }
  94.  
  95. /*
  96.  *----------------------------------------------------------------------
  97.  *
  98.  * Tcl_AppInit --
  99.  *
  100.  *    This procedure performs application-specific initialization.
  101.  *    Most applications, especially those that incorporate additional
  102.  *    packages, will have their own version of this procedure.
  103.  *
  104.  * Results:
  105.  *    Returns a standard Tcl completion code, and leaves an error
  106.  *    message in interp->result if an error occurs.
  107.  *
  108.  * Side effects:
  109.  *    Depends on the startup script.
  110.  *
  111.  *----------------------------------------------------------------------
  112.  */
  113.  
  114. int
  115. Tcl_AppInit(interp)
  116.     Tcl_Interp *interp;        /* Interpreter for application. */
  117. {
  118.     /*
  119.      * Call the init procedures for included packages.  Each call should
  120.      * look like this:
  121.      *
  122.      * if (Mod_Init(interp) == TCL_ERROR) {
  123.      *     return TCL_ERROR;
  124.      * }
  125.      *
  126.      * where "Mod" is the name of the module.
  127.      */
  128.  
  129.     if (Tcl_Init(interp) == TCL_ERROR) {
  130.     return TCL_ERROR;
  131.     }
  132.     if (Pltcl_Init(interp) == TCL_ERROR) {
  133.     return TCL_ERROR;
  134.     }
  135.  
  136. /* Application-specific startup.  That means: for use in pltcl ONLY. */
  137.  
  138. /* Rename "exit" to "tclexit", and insert custom exit handler */
  139.  
  140.     Tcl_VarEval(interp, "rename exit tclexit", (char *) NULL);
  141.  
  142.     Tcl_CreateCommand(interp, "exit", plExitCmd,
  143.                       (ClientData) NULL, (void (*)(ClientData)) NULL);
  144.  
  145. /* Initialize graphics package */
  146.  
  147.     plinit();
  148.  
  149. /* Make sure we are in text mode when interactively entering commands */
  150.  
  151.     Tcl_SetVar(interp, "tcl_prompt1", "pltext; puts -nonewline \"% \"", 0);
  152.  
  153. /* Also before an error is printed.  Can't use normal call mechanism here */
  154. /* because it trashes the interp->result string. */
  155.  
  156.     tclErrorHandler = plErrorHandler;
  157.  
  158.     return TCL_OK;
  159. }
  160.  
  161. /*
  162.  *----------------------------------------------------------------------
  163.  *
  164.  * plErrorHandler --
  165.  *
  166.  *    Handles error conditions while parsing.  Modified from the version
  167.  *    in tclMain.c to ensure we are on the text screen before issuing
  168.  *    the error message, otherwise it may disappear.
  169.  *
  170.  * Results:
  171.  *    None.
  172.  *
  173.  * Side effects:
  174.  *    Error info is printed to stdout, if interactive, otherwise stderr.
  175.  *
  176.  *----------------------------------------------------------------------
  177.  */
  178.  
  179. static void
  180. plErrorHandler(Tcl_Interp *interp, int code, int tty)
  181. {
  182.     pltext();
  183.     if (tty)
  184.     printf("%s\n", interp->result);
  185.     else
  186.     fprintf(stderr, "%s\n", interp->result);
  187. }
  188.